<?php
namespace Home\Controller;
use Common\Controller\CommonController;

class DealerController extends CommonController {

	public function index(){
		$dealer=M('dealer');
		
		$key=I('key');

		if(!empty($key)){
			$where['d.name'] = array('like','%'.$key."%");
			$arr = $key;
		}

		$count = $dealer->where($where)->count();
		$Page = new \Think\Pager($count,10);
		$show=$Page->showhtml();

		$list=$dealer->alias('d')
			->field('d.*')
			->where($where)->limit($Page->firstRow.",".$Page->listRows)->select();
		foreach ($list as $key => $value) {
			$list[$key]['key']=$key+1;
			try{
				$num=M('shop')->where(array('did'=>$value['id']))->select();
				$num=count($num);
			}catch( \Exception $e){
				$num=0;
			}
			$list[$key]['num']=$num;
		}
		
		$this->assign('page',$show);
		$this->assign('arr',$arr);
		$this->assign('list',$list);
		$this->display();

	}
	
	public function add(){
		$this->display();
	}

	public function doadd(){
		$name=I('name');
		$phone=I('phone');
		$admin_id = I('admin_id');
		$data = array(
			'name'			=> $name,
			'phone'			=> $phone,
			'create_time'	=> time(),
			'status'		=> 1,
		);
		$list=M('dealer')->data($data)->add();
		if($list){
			$this->success('添加成功!',U('Dealer/index'));
		}else{
			$this->error();
		}
	}
	public function show(){
		$id=I('id');
		//P($id);
		
		$count = M('shop')->where(array('did'=>$id))->count();
		$Page = new \Think\Pager($count,10);
		$show=$Page->showhtml();
		$list=M('shop')->where(array('did'=>$id))->limit($Page->firstRow.",".$Page->listRows)->select();
		$this->assign('page',$show);
		$this->assign('list',$list);
		$this->display();		

	}

	public function update(){
		$id=I('id');
		$list=M('dealer')->alias('d')
			->join('left join __RBAC_MANAGER__ rm on d.id=rm.dealer_id')
			->where(['d.id'=>$id])
			->field('d.*,rm.id as admin_id')->find();
		$admin_arr = M('rbac_manager')->alias('rm')->join('left join __RBAC_ROLE_MANAGER__ rrm on rm.id=rrm.user_id')->where(['rrm.role_id'=>$this->dealer_role_id, 'rm.status'=>1])->getField('rm.id,rm.username', true);
		$this->assign('admin_arr', $admin_arr);
		$this->assign('list',$list);
		$this->display();

	}
	public function doupdate(){
		$id = I('id');
		$name=I('name');
		$phone = I('phone');
		$admin_id = I('admin_id');

		$data = array(
			'id'=>$id,
			'name'=>$name,
			'phone'=>$phone,
		); 
		try{
			$list=M('dealer')->data($data)->save();
		}catch( \Exception $e){
			$this->error();
		}
		$this->success('修改成功!',U('Dealer/index'));
	
	}

	public function del(){
		$id=I('id');
		
		$list=M('dealer')->where(array('id'=>$id))->delete();
		
		if($list){
			$this->success('删除成功',U('Dealer/index'));
		}else{
			$this->error();
		}
	}

	//导出Excel
	public function export(){
		$name=I('name');
		
		if(!empty($name)){
			$where['name'] = array('like',"%".$name."%");
		}

		$str = "经销商,电话\r\n";

		$list=M("dealer")->where($where)->select();
		// P($list);

		foreach ($list as $key => $value) {
			$name = $value['name'];
			$phone = $value['phone'];
		
			$str .= ''.$name.','.$phone."\r\n";
		}
		$filename = "经销商信息".date("Ymd",time()).".csv";
		header("Content-type:text/csv");			//要将数据导出到本地即下在，需要修改header信息，
		header("Content-Disposition:attachment;filename=".$filename);
		header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
		header('Expires:0');
		header('Pragma:public');
		$str =iconv("UTF-8","gbk//TRANSLIT",$str); 
		echo $str;
	}
}